/**  
 * Project Name:framework  
 * File Name:BusinessLogAppen.java  
 * Package Name:com.org.framework.log  
 * Date:2017年8月3日下午2:39:22  
 * Copyright (c) 2017, 123774135@qq.com All Rights Reserved.  
 *  
*/  

/**    
  *@Title: BusinessLogAppen.java  
  *@Package com.org.framework.log  
  *@Description: TODO(用一句话描述该文件做什么)  
  *@author 123774135@qq.com    
  *@date 2017年8月3日 下午2:39:22  
  *@version V1.0    
  */
   
  
package com.bicon.base.data.common.log;  
/**  
 * ClassName:BusinessLogAppen <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2017年8月3日 下午2:39:22 <br/>  
 * @author   Administrator  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */

import java.util.concurrent.Callable;

import org.codehaus.jackson.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bicon.base.data.common.DbBuilder;
import com.bicon.base.data.common.StringTools;
import com.bicon.base.data.common.tools.JsonUtil;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.AppenderBase;

/**  
  *@ClassName: BusinessLogAppen  
  *@Description: 实现自定义logback日志，集成 AppenderBase重写append方法
  *@author 123774135@qq.com  
  *@date 2017年8月3日 下午2:39:22      
  */

public class BusinessLogAppen extends AppenderBase<LoggingEvent>{

	private Logger logger = LoggerFactory.getLogger(BusinessLogAppen.class);
	
	private String transMethod;
	
	

	
	public String getTransMethod() {
		return transMethod;
	}


	public void setTransMethod(String transMethod) {
		this.transMethod = transMethod;
	}


	DbBuilder dbBuilder ;
	
	
	public DbBuilder getDbBuilder() {
		return dbBuilder;
	}


	public void setDbBuilder(DbBuilder dbBuilder) {
		this.dbBuilder = dbBuilder;
	}


	private 
	
	ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(CrmLogAspect.THREAD_POOL_EXECUTOR);
	
	/**
	 * 解析json，
	  *@Title: parseJson  
	  *@Description: TODO(这里用一句话描述这个方法的作用)  
	  *@param @param json
	  *@param @param nodeName
	  *@param @return    设定文件  
	  *@return String    返回类型  
	  *@throws
	 */
	private String parseJson(String json,String nodeName) {
		 JsonNode jsonNode =  JsonUtil.getNode(json, nodeName);
		 return StringTools.mkString(jsonNode.getTextValue());
	}
		//processData(listeningExecutorService,eventObject.getMessage().);
	

		 @Override
		 public void append(LoggingEvent eventObject) {
			 String jsonStr = eventObject.getFormattedMessage();
			 //BusinessLogEntity businesslogentity = (BusinessLogEntity)JsonUtil.jsonToObject(jsonStr, BusinessLogEntity.class);
			 //System.out.println("----------"+businesslogentity);
			 String operater = this.parseJson(jsonStr, "operater");
			 String bus_name = this.parseJson(jsonStr, "bus_name");
			 String befor_hander = this.parseJson(jsonStr, "befor_hander");
			 String after_hander = this.parseJson(jsonStr, "key");
			 String result = this.parseJson(jsonStr, "result");
			 String reson = this.parseJson(jsonStr, "reson");
			 BusinessLogEntity businesslogentity = new BusinessLogEntity(operater,bus_name,befor_hander,after_hander,result,reson);
             processData(listeningExecutorService,businesslogentity);

		 }

	
	
	private void processData(final ListeningExecutorService listeningExecutorService,final BusinessLogEntity businessLogEntity) {
		ListenableFuture<String> listenableFuture =	listeningExecutorService.submit(new Callable<String>() {

			@Override
			public String call() throws Exception {
				  
				//进行入库操作
			   String after_json =	businessLogEntity.getKey();
			   String befor_json = businessLogEntity.getBefor_hander();
			   String adminNo = businessLogEntity.getOperater();
			   String result = businessLogEntity.getResult();
			   String reson = businessLogEntity.getReson();
			   String datetime = businessLogEntity.getDatetime();
			   String id = businessLogEntity.getId();
			   String bus_name = businessLogEntity.getBus_name();
			   int res = 1;
//			   String sql = "INSERT INTO SYS_BUSINESS_LOG(ID, OPERATER ,BUS_NAME ,BEFOR_HANDER, AFTER_HANDER,RESULT,RESON, DATETIME) VALUES(?,?,?,?,?,?,?,?)";
//			   Object [] params = {id,adminNo,bus_name,befor_json,after_json,result,reson,datetime};
//			   int res = dbBuilder.save(sql, params);
//			   System.out.println(res);
			   return res == 1?"业务日志入库成功":"业务日志入库失败";
				
			}
		});
		Futures.addCallback(listenableFuture,  new FutureCallback<String>(){
			@Override
			public void onSuccess(String result) {
				if(logger.isDebugEnabled()) {
					logger.debug("获得最后结果:"+result);
				}
				System.out.println("获得最后结果:"+result);
				
			}
			@Override
			public void onFailure(Throwable t) {
				System.out.println(t.getCause());
			}
			
		});
	}


	


}
  
